<?php

namespace app\common\controller;

use app\common\model\cms\ClassModel;
use think\Controller;
use think\facade\Cache;

class HomeBaseController extends Controller {
    /**
     * @var bool 验证失败是否抛出异常
     */
    protected $failException = true;
    /**
     * 模型对象
     * @var \think\Model
     */
    protected $model = null;

    protected $cmsClass = null;

    protected $webconfig;
    /**
     * 初始化方法
     * AdminBaseController constructor.
     */
    public function __construct() {
        parent::__construct();
        $this->webconfig = Cache::get('webconfig');
        $classModel = new ClassModel();
        $theme = $this->webconfig['theme'];
        $viewPath = "themes".DIRECTORY_SEPARATOR."{$theme}".DIRECTORY_SEPARATOR;
        $viewMobilePath = "themes".DIRECTORY_SEPARATOR."{$theme}_mobile".DIRECTORY_SEPARATOR;
        $this->view->config('view_depr', '_'); //设置模板分隔符
        //PC端主题设置
        $this->view->config('view_path', $viewPath);
        $realMobile = 0;
        //手机端主题设置
        if ($this->request->isMobile()) {
            $this->view->config('view_path', $viewMobilePath);
            $realMobile = 1;
            if (!$this->view->exists('')) { //不存在手机模板使用PC模板
                $this->view->config('view_path', $viewPath);
                $realMobile = 0;
            }
        }
        //视图过滤
        $this->view->filter(function($content) use($theme,$realMobile){
            if($realMobile===0){
                return str_replace("static_skins/","/themes/{$theme}/static_skins/",$content);
            }else{
                return str_replace("static_skins/","/themes/{$theme}_mobile/static_skins/",$content);
            }
        });
        $this->cmsClass = $classModel->getList();
        $this->assign('webconfig',$this->webconfig);
        $this->assign('cms_class',$this->cmsClass);
    }

}
